//
//  HLNavigationController.m
//  HouLu
//
//  Created by WANGGNIT on 2019/1/7.
//  Copyright © 2019 曦岸. All rights reserved.
//

#import "HLNavigationController.h"

@interface HLNavigationController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>

@end

@implementation HLNavigationController
/**
 *
 */
+ (void)initialize
{
    // 1.设置导航栏主题
    [self setupNavBarTheme];
}

/**
 *  设置导航栏主题
 */
+ (void)setupNavBarTheme
{
    // 取出appearance对象
    
    UINavigationBar *navBar = [UINavigationBar appearance];
    
    navBar.translucent = NO;
    
    [navBar setBackgroundColor:HLHomeColor];

    navBar.layer.shadowColor = [UIColor blackColor].CGColor;
    
    navBar.layer.shadowOffset = CGSizeMake(0,1);
    
    navBar.layer.shadowOpacity = 0.2;
    
    [navBar setTitleTextAttributes:@{NSForegroundColorAttributeName : HLBlackColor}];
    
}

/**
 *  设置导航栏按钮主题
 */
+ (void)setupBarButtonItemTheme
{}

/**
 * 可以在这个方法中拦截所有push进来的控制器
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count > 0) { //如果push进来的不是第一个控制器
        // 隐藏tabbar
        viewController.hidesBottomBarWhenPushed = YES;
    }
    // 这句super的push要放在后面, 让viewController可以覆盖上面设置的leftBarButtonItem
    [super pushViewController:viewController animated:animated];
}

- (void)viewDidLoad {
   
    [super viewDidLoad];
    
    self.delegate = self;
    
    __weak typeof (self)weakSelf = self;
    
    if([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        self.interactivePopGestureRecognizer.delegate = weakSelf;
    }
}

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    if (self.navigationController.viewControllers.count == 1){
        return NO;
    }else{
        return YES;
    }
}

-(void)navigationController:(UINavigationController*)navigationController didShowViewController:(UIViewController*)viewController animated:(BOOL)animated{
    
    if([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        navigationController.interactivePopGestureRecognizer.enabled=YES;
        }
    //使navigationcontroller中第一个控制器不响应右滑pop手势
    if(navigationController.viewControllers.count ==1){
        navigationController.interactivePopGestureRecognizer.enabled=NO;
        navigationController.interactivePopGestureRecognizer.delegate=nil;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
